package com.gutai.admin.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gutai.admin.common.entity.dto.CategoryLevelDto;
import com.gutai.admin.common.entity.dto.LevelDto;
import com.gutai.admin.common.entity.response.Response;
import com.gutai.admin.common.util.ExceptionUtils;
import com.gutai.admin.common.util.ResponseHelper;
import com.gutai.admin.entity.Category;
import com.gutai.admin.service.ICategoryService;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);
    @Autowired
    private ICategoryService categoryService;

    @CrossOrigin
    @GetMapping("/listAll")
    public Response listAll(String categoryName) {
        try {
            List<Category> list = null;
            if (StringUtils.isNotBlank(categoryName)) {
                list = categoryService.getList(categoryName);
            } else {
                list = categoryService.getAll();
            }
            return ResponseHelper.buildOk(list);
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/selectList")
    public Response selectList() {
        try {
            List<CategoryLevelDto> list = categoryService.getRelationCategory();
            return ResponseHelper.buildOk(list);
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }
    
    @CrossOrigin
    @GetMapping("/findAllParent")
    public Response findAllParent() {
        try {
            List<Category> list = categoryService.findAllParent();
            return ResponseHelper.buildOk(list);
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/findAllLevel")
    public Response findAllLevel() {
        try {
            List<LevelDto> list = categoryService.findAllLevel();
            return ResponseHelper.buildOk(list);
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping("/add")
    public Response add(@RequestBody Category entity) {
        try {
            categoryService.add(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败");
        }
    }

    @CrossOrigin
    @PostMapping("/update")
    public Response update(@RequestBody Category entity) {
        try {
            categoryService.update(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败");
        }
    }

    @CrossOrigin
    @GetMapping("/delete")
    public Response delete(Integer id) {
        try {
            categoryService.delete(id);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败");
        }
    }
//    @CrossOrigin
//    @GetMapping("/relation")
//    public Response getRelationship(Integer id) {
////        int rowNum = categoryService.delete(id);
////        if (rowNum > 0) {
////            return ResponseHelper.buildOk(rowNum);
////        }
//        return ResponseHelper.buildFail("操作DB失败");
//    }

}
